#
# Copyright (C) [2024] Xingyun Integrated Circuit, Inc.
#
# GreenCode was a private technology asset of Xingyun Integrated Circuit， Inc （Confidential）
# Author: Shawn.Tan
# Date: 2025.10.28
#
# History: Initial Version 2025.10.28
#
#
from gpgpu.EnvGPGPU import EnvGPGPU
from gpgpu.GenThreadGPGPU import GenThreadGPGPU
from base.Sequence import Sequence
import MemoryTraits
import RandomUtils

# This test verifies that memory attributes can be set on specific regions of memory.
class MainSequence(Sequence):
    def generate(self, **kargs):
        for _ in range(50):
            size_bits = RandomUtils.random32(1, 20)
            size = 2 ** size_bits
            align_bits = RandomUtils.random32(1, size_bits)
            align = 2 ** align_bits
            arch_mem_attr = self.choice(
                ("MainRegion", "IORegion", "CacheableShared", "Uncacheable")
            )
            impl_mem_attr = self.choice(("DMA Controller", "UART 0", "UART 1", "DDR Control"))

            start_addr = self.genPA(
                Size=size,
                Align=align,
                Type="D",
                MemAttrArch=arch_mem_attr,
                MemAttrImpl=impl_mem_attr,
            )
            end_addr = start_addr + size - 1

            if not MemoryTraits.hasMemoryAttribute(arch_mem_attr, start_addr, end_addr):
                self.error(
                    "Memory attribute %s not assigned to physical address range 0x%x-0x%x"
                    % (arch_mem_attr, start_addr, end_addr)
                )

            if not MemoryTraits.hasMemoryAttribute(impl_mem_attr, start_addr, end_addr):
                self.error(
                    "Memory attribute %s not assigned to physical address range 0x%x-0x%x"
                    % (impl_mem_attr, start_addr, end_addr)
                )


MainSequenceClass = MainSequence
GenThreadClass = GenThreadGPGPU
EnvClass = EnvGPGPU
